/*
//半全场
{
  cat:'半全场', 
  context : {odd:0.8, beton:'主/和', result:'1:0/3:2'},
  amount : 1000
}
//波胆
{
  cat:'波胆', 
  context : {odd:0.8, beton:'3:2', result:'3:2'},
  amount : 1000
}
//入球数
{
  cat:'入球数',
  context : {odd:0.8, beton:'5', result:'3:2'},
  amount : 1000
}
//独赢
{
  cat:'独赢', 
  context : {odd:0.8, beton:'主', result:'3:2'},
  amount : 1000
}
//让球
{
  cat:'让球', 
  context : {odd:0.8, beton:'主', pankou:'0.5/1', who2who:'主让客', result:'3:2'},
  amount : 1000
}
//大小
{
  cat:'大小', 
  context : {odd:0.8, beton:'大', pankou:'3.5/4', result:'3:2'},
  amount : 1000
}
//单双
{
  cat:'单双', 
  context : {odd:0.8, beton:'单', result:'3:2'},
  amount : 1000
}
//过关, 每场比赛只能赌一注.
{
  cat:'过关',
  context : [{odd:0.8, beton:'单', cat:'单双', result:'3:2'}, {odd:0.8, pankou='2.5', cat:'大小',beton:'大', attach:'5', result:'3:2'}],
  amount : 1000
}
//滚球, 不完全相同于让球与大小, 主要是比分判断方面, 比分依据为最终比分减去当时比分.
{
  context : [{cat:'大小', odd:0.8, beton:'大', result:'1:0/3:2'}],
  amount : 1000
}
*/

onerror = handler;

function calc(abet)
{
  abet = "var bet=" + abet +";";//wrap it.
  eval(abet);
  var ctx = bet.context;
  switch(bet.cat)
  {
  case "入球数" :
    return 入球数(bet);
  case "单双" :
    return 单双(bet);
  case "波胆" :
    return 波胆(bet);
  case "独赢" :
    return 独赢(bet);
  case "半全场" :
    return 半全场(bet);
  case "大小" :
    return 大小(bet);
  case "让球" :
    return 让球(bet);//如果是主让客队...主队分数减去让球数.让球数分为2个, 通过'/'分隔的.
  case "过关" :
    return 过关(bet);
  }
}

function 入球数(bet)
{
  var total = eval(bet.context.result.replace(":", "+"));
  if(bet.context.beton.search('7') > -1)//大于等于7
    if(total >= 7) return bet.context.odd*bet.amount;
    else return 0;
  else if(bet.context.beton.search('6') > -1)//4-6
    if(total>=4 && total<=6) return bet.context.odd*bet.amount;
    else return 0;
  else if(bet.context.beton.search('3') > -1)//2-3
    if(total>=2 && total<=3) return bet.context.odd*bet.amount;
    else return 0;
  else 
    if(total >=0 && total <= 1) return bet.context.odd*bet.amount;
  return 0;
}

function 单双(bet)
{
  if(bet.context.beton == (eval(bet.context.result.replace(":", "+"))%2?"单":"双"))
    return bet.context.odd*bet.amount;
  else return 0;
}

function 波胆(bet)
{
  var rs = bet.context.result.split(':');
  if(rs[0] > 4 || rs[1] > 4)
    if(bet.context.beton == "x:x")return bet.context.odd*bet.amount;
    else return 0;
  if(bet.context.beton == bet.context.result)
    return bet.context.odd*bet.amount;
  else return 0;
}

function 独赢(bet)
{
  if(bet.context.beton == calcr(bet.context.result))
    return bet.context.odd*bet.amount;
  else return 0;
}

function 半全场(bet)
{
  var r = bet.context.result.split('/');
  var rr = calcr(r[0]) + "/" + calcr(r[1]);
  if(bet.context.beton == rr)
    return bet.context.odd*bet.amount;
  else return 0;
}

function 大小(bet)
{
  var r = 0.0;
  var sum = eval(bet.context.result.replace(":", "+"));
  if(bet.context.pankou.search('/') != -1){
    var a = bet.context.pankou.split('/');
    if(bet.context.beton == "大"){
      if(sum>a[0])r += (bet.amount/2)*bet.context.odd;
        else if(sum == a[0]) r += bet.amount/2;
      if(sum>a[1])r += (bet.amount/2)*bet.context.odd;
        else if(sum == a[1]) r += bet.amount/2;
    }
    if(bet.context.beton == "小"){
      if(sum<a[0])r += (bet.amount/2)*bet.context.odd;
        else if(sum == a[0]) r += bet.amount/2;
      if(sum<a[1])r += (bet.amount/2)*bet.context.odd;
        else if(sum == a[1]) r += bet.amount/2;
    }
  }
  else{
    if(bet.context.beton == "大"){
      if(sum>bet.context.pankou)r = (bet.amount)*bet.context.odd;
        else if(sum == bet.context.pankou) r = bet.amount;
    }
    if(bet.context.beton == "小"){
      if(sum<bet.context.pankou)r = (bet.amount)*bet.context.odd;
        else if(sum == bet.context.pankou) r = bet.amount;
    }
  }
  return r;
}

function 让球(bet)
{
  var r = 0.0;
  if(bet.context.who2who == "主让客"){
    var sum = eval(bet.context.result.replace(":", "-"));
    if(bet.context.pankou.search('/') != -1){
      var a = bet.context.pankou.split('/');
      if(bet.context.beton == "主"){
        if(sum>a[0])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[0]) r += bet.amount/2;
        if(sum>a[1])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[1]) r += bet.amount/2;
      }
      if(bet.context.beton == "客"){
        if(sum<a[0])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[0]) r += bet.amount/2;
        if(sum<a[1])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[1]) r += bet.amount/2;
      }
    }
    else{
      if(bet.context.beton == "主"){
        if(sum>bet.context.pankou)r = (bet.amount)*bet.context.odd;
          else if(sum == bet.context.pankou) r = bet.amount;
      }
      if(bet.context.beton == "客"){
        if(sum<bet.context.pankou)r = (bet.amount)*bet.context.odd;
          else if(sum == bet.context.pankou) r = bet.amount;
      }
    }
  }
  else{
    var sum = eval(bet.context.result.replace(":", "-"));
    sum = -sum;
    if(bet.context.pankou.search('/') != -1){
      var a = bet.context.pankou.split('/');
      if(bet.context.beton == "客"){
        if(sum>a[0])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[0]) r += bet.amount/2;
        if(sum>a[1])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[1]) r += bet.amount/2;
      }
      if(bet.context.beton == "主"){
        if(sum<a[0])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[0]) r += bet.amount/2;
        if(sum<a[1])r += (bet.amount/2)*bet.context.odd;
          else if(sum == a[1]) r += bet.amount/2;
      }
    }
    else{
      if(bet.context.beton == "客"){
        if(sum>bet.context.pankou)r = (bet.amount)*bet.context.odd;
          else if(sum == bet.context.pankou) r = bet.amount;
      }
      if(bet.context.beton == "主"){
        if(sum<bet.context.pankou)r = (bet.amount)*bet.context.odd;
          else if(sum == bet.context.pankou) r = bet.amount;
      }
    }
  }
  return r;
}

function 过关(bets){
  var r = 1.0;
  var amount = bets.amount;
  for(item in bets.context){
    var str = "var bet = {cat:'" + bets.context[item].cat + "', context:{}, amount:" + 1.0 + "}";
    eval(str);
    bet.context=bets.context[item];
    var rt = eval(bets.context[item].cat + "(bet)");
    if(rt == 0.0) return 0.0;//只要有一场比赛输了, 就全输.
    r *= rt;
  }
  r *= amount;
  return r;
}


//str is '3:2', '4:0'..., the result is '主', '客' or '和'
function calcr(str)
{
    var home = eval(str.replace(":", ">"));
    var draw = eval(str.replace(":", "=="));
    return (home ? "主" : draw ? "和" : "客");
}



/////////////////////////////////////////////////////////////////////
//if error occurs, this function will handle it, so try java code if you want process it outside js code.
function handler(msg, url, line)
{
  print("in 算法.js : 赔率算法出现异常!!\n" + "msg : " + msg + "\nline : " + line);
  return msg + line;
}


function test(msg)
{
  eval("var c=4.5;");
  return c;
}
println("成功启动赔率算法引擎...");